package com.yidu.service.impl;

import com.yidu.entity.WorkUser;
import com.yidu.mapper.WorkUserMapper;
import com.yidu.service.WorkUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 任俊杰
 * @version 1.0
 * @date 2022/6/20
 */
@Service

public class userImpl implements WorkUserService {
    @Resource
    private WorkUserMapper workUserMapper;

    //查询+分页
    public List<WorkUser> query( int page , int limit,WorkUser workUser) {
        Map map=new HashMap();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        map.put("strWhere",buildStrWhere(workUser));
        return workUserMapper.query(map);
    }



    /**
     * 生成条件字符串
     * @param workUser 生成条件的参数
     * @return 条件字符串  and getRightText like '%%'
     */
    public String buildStrWhere(WorkUser workUser){
        StringBuffer strWhere=new StringBuffer("");
        if(workUser.getUsername()!=null && !workUser.getUsername().equals(""))
        {
            strWhere.append(" and username like '%" +workUser.getUsername() + "%'");
        }
        if(workUser.getUserid()!=null && workUser.getUserid()!=0)
        {
            strWhere.append(" and userid= " +workUser.getUserid());//userid 用户编号（id）
        }
        System.out.println(strWhere+"===111");
        return  strWhere.toString();
    }



    //查行数 +名字查（按名字模糊查）
    public int queryCount(WorkUser workUser) {
        Map map=new HashMap();
        map.put("strWhere",buildStrWhere(workUser));
        int count =workUserMapper.queryCount(map);
        return count;
    }

    //新增
    public int addUser(WorkUser workUser) {
        return workUserMapper.addUser(workUser);
    }

    //修改
    public int updateUser(WorkUser workUser) {
        return workUserMapper.updateUser(workUser);
    }

    //删除
    public int deleteUser(String userid) {
        //将id转为数据
        String[] split = userid.split(",");
        //创建存放id的集合
        ArrayList<Object> equityDataIdList = new ArrayList<Object>();
        for (String id : split) {
            //将id存入集合
            equityDataIdList.add(id);
        }
        return workUserMapper.deleteUser(equityDataIdList);
    }



    //单个删除
   /* @Override
    public int deleteUser1(Integer userid) {
        return workUserMapper.deleteUser1(userid);
    }*/

}
